package com.ibiaoke.marketing.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.marketing.entity.Marketing;
import com.ibiaoke.marketing.entity.MarketingExample;
import com.ibiaoke.marketing.mapper.MarketingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class MarketingDao extends BaseDao<Marketing, MarketingExample, Integer, MarketingMapper> {

    @Autowired
    private MarketingMapper marketingMapper;

    @Override
    public MarketingMapper getMapper() {
        return marketingMapper;
    }

    public List<Marketing> listMarketing(String marketingName, Page page) {
        MarketingExample example = new MarketingExample();

        MarketingExample.Criteria criteria = example.createCriteria().andDelStatusEqualTo(CommonConsts.NO);
        if(marketingName != null) {
            criteria.andMarketingNameLike("%" +marketingName+ "%");
        }
        page.setTotalCount(countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("update_time");
        return selectByExample(example);
    }

    public List<Marketing> listValidMarketing(Integer type) {
        MarketingExample example = new MarketingExample();
        example.createCriteria().andTypeEqualTo(type).andDelStatusEqualTo(CommonConsts.NO)
                .andBeginTimeLessThanOrEqualTo(new Date()).andEndTimeGreaterThan(new Date());
        example.setOrderByClause("begin_time asc");
        return selectByExample(example);
    }

    public int deleteMarketing(Integer marketingId) {
        MarketingExample example = new MarketingExample();
        example.createCriteria().andMarketingIdEqualTo(marketingId);
        Marketing marketing = new Marketing();
        marketing.setDelStatus(CommonConsts.YES);
        marketing.setUpdateTime(new Date());
        return updateByExampleSelective(marketing, example);
    }
}
